package _04io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @description:
 * @author: songtao@cskaoyan.onaliyun.com
 **/

/*
有这样的一个words数组，数组中每个字符串的格式为“词性:单词”

String[] words = {"verb:eat","verb:drink","verb:sleep","verb:play","noun:rice","noun:meat","noun:hand","noun:hair"};

根据单词性质动词verb全部存入verb.txt文件中

根据单词性质名词noun全部存入noun.txt文件中
 */
public class Work3 {
    public static void main(String[] args) {

        String[] words = {"verb:eat", "verb:drink", "verb:sleep", "verb:play"
                , "noun:rice", "noun:meat", "noun:hand", "noun:hair"};

        String verb = "verb";
        String noun = "noun";
        //将所有的动词和名词分别拼接到verbs和nouns两个字符串中
        //让后直接将包含所有 动词的字符串verbs 和 包含所有 名词的字符串nouns，分别写入verb.txt和noun.txt中
        String verbs = "";
        String nouns = "";
        for (String word : words) {

            if (word.contains(verb)) {

                verbs += word;

                verbs += "\n";

            }

            if (word.contains(noun)) {

                nouns += word;

                nouns += "\n";

            }

        }

        File verbFile = new File("D:\\WorkSpace\\verb.txt");

        File nounFile = new File("D:\\WorkSpace\\noun.txt");

        FileOutputStream verbFileOutputStream = null;

        FileOutputStream nounFileOutputStream = null;

        try {

            //初始化输出流

            verbFileOutputStream = new FileOutputStream(verbFile);

            nounFileOutputStream = new FileOutputStream(nounFile);

            //获得字节数组

            byte[] verbBytes = verbs.getBytes();

            byte[] nounBytes = nouns.getBytes();

            //写入文件

            verbFileOutputStream.write(verbBytes);

            nounFileOutputStream.write(nounBytes);

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            //资源释放

            try {
                if (verbFileOutputStream != null) {
                    verbFileOutputStream.close();
                }
                if (nounFileOutputStream != null) {
                    nounFileOutputStream.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }


}
